// 定义树节点比较函数
function isNodeToRemove(node) {
    return (node === "/res_button" || node === "/res_menu" || node === "/res_api")


    ;
}

// 移除树结构中的数据
function changeVal(treeData, p = "", array = [] | null) {
    p = (p + "/" + treeData.path).replace(/\/+/g, '/');
    console.log(p)

    if (array) {
        if (isNodeToRemove(p)) {
            // var indexOf = array.indexOf(treeData);
            // array.splice(indexOf, 1)
            treeData.bbb = true
        }
    }


    // 遍历当前节点的子节点
    if (treeData.children) {
        treeData.children = treeData.children.map(child => {
            // 递归调用 removeData 函数来移除子节点中的数据
            return changeVal(child, p, treeData.children);
        });

        // 过滤掉值为 null 的子节点
        treeData.children = treeData.children.filter(child => child !== null);
    }

    // 返回更新后的当前节点
    return treeData;

}


// 定义树结构的数据
var treeData = [{
    "name": "dashboard",
    "path": "/dashboard",
    "meta": {"locale": "导航", "requiresAuth": true, "icon": "icon-dashboard", "order": 0},
    "children": [{
        "path": "workplace",
        "name": "Workplace",
        "meta": {"locale": "欢迎页", "requiresAuth": true, "roles": ["*"]},
        "children": []
    }]
}, {
    "name": "biz",
    "path": "/",
    "meta": {"locale": "系统管理", "requiresAuth": true, "icon": "icon-dashboard", "order": 1},
    "children": [{
        "path": "user",
        "name": "user_manager",
        "meta": {"locale": "用户管理", "requiresAuth": true, "roles": ["*"]},
        "children": []
    }, {
        "path": "dept",
        "name": "dept_manager",
        "meta": {"locale": "部门管理", "requiresAuth": true, "roles": ["*"]},
        "children": []
    }, {
        "path": "role",
        "name": "role_manager",
        "meta": {"locale": "角色管理", "requiresAuth": true, "roles": ["*"]},
        "children": []
    }, {
        "path": "dict",
        "name": "dict_manager",
        "meta": {"locale": "字典管理", "requiresAuth": true, "roles": ["*"]},
        "children": []
    }, {
        "path": "dict_item",
        "name": "dict_item_manager",
        "meta": {"locale": "字典项管理", "requiresAuth": true, "roles": ["*"]},
        "children": []
    }, {
        "path": "res_menu",
        "name": "res_menu_manager",
        "meta": {"locale": "菜单管理", "requiresAuth": true, "roles": ["*"]},
        "children": []
    }, {
        "path": "res_button",
        "name": "res_button_manager",
        "meta": {"locale": "按钮管理", "requiresAuth": true, "roles": ["*"]},
        "children": []
    }, {
        "path": "res_api",
        "name": "res_api_manager",
        "meta": {"locale": "接口管理", "requiresAuth": true, "roles": ["*"]},
        "children": []
    }]
}];

treeData.forEach((e) => {
    e = changeVal(e);

})



function removeItemsWithBbb(data) {
    return data.reduce((acc, item) => {
        if (!(item.hasOwnProperty("bbb") && item.bbb === true)) {
            const children = item.children ? removeItemsWithBbb(item.children) : [];
            acc.push({ ...item, children });
        }
        return acc;
    }, []);
}


// 执行删除操作
var removeItemsWithBbb1 = removeItemsWithBbb(treeData);




console.log(JSON.stringify(removeItemsWithBbb1));
